package com.itheima.d2;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;

public class StreamAPI {
    public static void main(String[] args) {
        Collection<String> list = new ArrayList<>();
        Collections.addAll(list,"张无忌","张三丰","周芷若","赵敏");
        System.out.println(list);
        // 1. 集合获取流
        Stream<String> stream = list.stream();
        // Stream<T> filter(Predicate<? super T> predicate);
        stream.filter(new Predicate<String>() {
            @Override
            public boolean test(String s) {
                if (s.startsWith("张")) {
                    return true;
                }
                return false;
            }
        }).forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                if(s.length() == 3){
                    System.out.println(s);
                }
            }
        });
        long count = list.stream().filter(new Predicate<String>() {
            @Override
            public boolean test(String s) {
                return s.startsWith("张");
            }
        }).count();
        System.out.println(count);
        System.out.println("=========");
        // 3.limit;
        list.stream().limit(2).forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        });
        System.out.println("========");
        // 4. skip
        list.stream().skip(2).forEach((String s) -> {
                System.out.println(s);
        });
        // 5. 加工方法 这个不是集合 比如说修改元素
        // 需求： 给集合元素的前面加上一个“黑马的”字符串
        // 第一个String表示原始数据的类型
        // 第二个String表示加工后的类型
        // map也是遍历
        System.out.println("=======");
        list.stream().map((String s) -> {
                s= "黑马的"+s;
                return s;
        }).forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        });
        //6 需求，把所有的名称 加工成一个学生对象
        System.out.println("======");
        list.stream().map(new Function<String, Student>() {

            @Override
            public Student apply(String s) {
                Student student = new Student();
                student.setName(s);
                return student;
            }
        }).forEach(new Consumer<Student>() {
            @Override
            public void accept(Student student) {
                System.out.println(student);
            }
        });

        // 7 合并两个流
        Stream<String> stream1 = list.stream().filter((String s) -> {
            return s.startsWith("张");
        });
        Stream<String> stream2 = Stream.of("Java", "Python");
        Stream<String> concat = Stream.concat(stream1, stream2);
        concat.distinct().forEach(System.out::println);
       // concat.forEach(System.out::println);
        // 8 去重复  .distinct()

    }
}
class Student{
   private String name;

    public Student() {
    }

    public Student(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                '}';
    }
}

